/**
 * 
 */
package it.unical.canonaco.rende.igpe.game.cbv.guilayer.mainmenu;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import it.unical.canonaco.rende.igpe.game.cbv.guilayer.MainFrame;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.components.OverallGUILayoutFactory;
import it.unical.canonaco.rende.igpe.game.cbv.guilayer.gamemanagermenu.GameManagerMenu;
import it.unical.canonaco.rende.igpe.game.cbv.logiclayer.engine.component.filehandlers.PlayerFileHandler;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

/**
 * @author Massimo Canonaco
 *
 */
public abstract class PlayerInfoDailog extends JPanel {

	/** */
	static private Image backgroundImage;
	
	/** */
	static protected GridBagConstraints layoutConstraints;

	/** */
	static private GridBagLayout layout;
	
	/** */
	private JDialog dialog;
	
	/** */
	static private LinkedList<String> existingProfiles;
	
	/** */
	private int dialogWidth;
	
	/** */
	private int dialogHeight;
	
	/** */
	protected MainMenuPanel parent;
	
	/** */
	static private GameManagerMenu gameManagerMenu;

	/** */
	protected float fontSize;
	
	/** */
	protected JButton backButton;
	
	/** */
	protected JButton goButton;
	
	/** */
	protected String playerName;
	
	/** */
	protected JPanel buttons;
	
	/**
	 * @throws IOException  */
	public PlayerInfoDailog(MainMenuPanel container, Map infoMap, OverallGUILayoutFactory factory) throws IOException
	{
		this.parent = container;
		setOpaque(false);
		final String BACKGROUND_IMAGE = "dialog.background.image";
		
		// If constraints are not initialized, create them.
		if(layoutConstraints == null)
		{
			// If layout's constraints are null, so layout is null too.
			layout = new GridBagLayout();
			
			// Same reason the background image.
			backgroundImage = (new ImageIcon((String)infoMap.get(BACKGROUND_IMAGE))).getImage();
			
			layoutConstraints = new GridBagConstraints();
			layoutConstraints.fill = GridBagConstraints.NONE;
			layoutConstraints.weightx = 1;
			layoutConstraints.weighty = 0;
			layoutConstraints.gridx = 0;
			layoutConstraints.insets = new Insets(5, 0, 5, 0);
			layoutConstraints.gridy = GridBagConstraints.RELATIVE;
		}
		
		
		// Set layout manager
		setLayout(layout);
		
		// Key values
		final String BUTTON_TEXT_SIZE = "dialog.button.text.size",
				BUTTON_WIDTH = "dialog.button.width",
				BUTTON_HEIGHT = "dialog.button.height",
				FONT_TEXT_HEIGHT = "dialog.text.size",
				DIALOG_WIDTH = "dialog.width",
				DIALOG_HEIGHT = "dialog.height";

		// Get the size for the dialog window, and set it to the values.
		dialogWidth = Integer.parseInt((String)infoMap.get(DIALOG_WIDTH));
		dialogHeight = Integer.parseInt((String)infoMap.get(DIALOG_HEIGHT));
				
		// Get the font size for the buttons.
		float fontSize = Float.parseFloat((String)infoMap.get(BUTTON_TEXT_SIZE));
		
		// Buttons' width and height.
		int width = Integer.parseInt((String)infoMap.get(BUTTON_WIDTH)),
				height = Integer.parseInt((String)infoMap.get(BUTTON_HEIGHT));
		
		Dimension buttonSize = new Dimension(width, height);
		
		// Get the font size for the dialog messages.
		this.fontSize = Float.parseFloat((String)infoMap.get(FONT_TEXT_HEIGHT));
		
		// Create and set buttons.
		backButton = factory.createButton(fontSize, "Indietro", width, height);
		backButton.setIcon(backButton.getSelectedIcon());
		backButton.setMaximumSize(buttonSize);
		backButton.setPreferredSize(buttonSize);
		backButton.setMinimumSize(buttonSize);
		backButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) 
			{
				closeDialog();
			}
		});
		backButton.setEnabled(true);
		
		goButton = factory.createButton(fontSize, "Vai", width, height);
		goButton.setIcon(goButton.getSelectedIcon());
		goButton.setMaximumSize(buttonSize);
		goButton.setPreferredSize(buttonSize);
		goButton.setMinimumSize(buttonSize);
		goButton.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) 
			{
				closeDialog();
				next();
			}
		});
		
		buttons = new JPanel(new FlowLayout());
		buttons.setOpaque(false);
		goButton.setEnabled(false);
		buttons.add(backButton);
		buttons.add(goButton);
	}
	
	/** */
	public List<String> getProfilesList() {
		
		if(existingProfiles == null)
		{
			existingProfiles = new LinkedList<>();
		}
		
		PlayerFileHandler loader = parent.getParentMainFrame().getGameManager().getProfileLoader();
		File[] files = new File(loader.getProfileFolder()).listFiles(new FilenameFilter() {

			@Override
			public boolean accept(File arg0, String arg1) 
			{
				return arg1.endsWith(PlayerFileHandler.EXTENSION);
			}
		});
		
		existingProfiles.removeAll(existingProfiles);
		
		String fileName = null;
		int extensionLenght = PlayerFileHandler.EXTENSION.length();
		
		for (File file : files) 
		{
			fileName = file.getName();
			existingProfiles.add(fileName.substring(0, fileName.length() - extensionLenght));
		}
		
		return existingProfiles;
	}
	
	@Override
	public void paint(Graphics g) 
	{
		Graphics2D g2 = (Graphics2D) g;
		g2.drawImage(backgroundImage,0, 0, dialogWidth, dialogHeight,null);
		super.paint(g);
	}
	
	/** */
	public void showDialog(Component parent, String title)
	{
		if(dialog == null)
		{
			dialog = new JDialog(findOwnerFrame(parent), true);
			dialog.setSize(dialogWidth, dialogHeight);
			dialog.setPreferredSize(new Dimension(dialogWidth, dialogHeight));
			Frame daddy = this.parent.getParentMainFrame();
			
			dialog.setBounds((daddy.getWidth()-dialogWidth)/2, (daddy.getHeight()-dialogHeight)/2, dialogWidth, dialogHeight);
			dialog.setUndecorated(true);
			dialog.add(this);
			dialog.getRootPane().setDefaultButton(backButton);
			dialog.pack();
			dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		}
		
		dialog.setVisible(true);
		dialog.requestFocusInWindow();
	}

	/** */
	public void closeDialog()
	{
		dialog.setVisible(false);
		dialog.dispose();
	}
	
	/** */
	public void next()
	{
		final MainFrame mainFrame = parent.getParentMainFrame();	
		if(gameManagerMenu == null)
		{
		try {
			gameManagerMenu = new GameManagerMenu(mainFrame);
			mainFrame.setGameManagerMenu(gameManagerMenu);
//			System.out.println("Istanziato game manager menu");
		} catch (IOException e) {
			e.printStackTrace();
		}
		}
//		mainFrame.getGameManager().getClientBattlePlayer()
		gameManagerMenu.showGM(false);
//		mainFrame.switchTo(gameManagerMenu.getCardPanel());
	}

	protected Frame findOwnerFrame(Component component)
	{
		Frame ownerFrame= null;
		
		if(component instanceof Frame)
		{
			ownerFrame = (Frame) component;
		}
		else
		{
			ownerFrame = (Frame) SwingUtilities.getAncestorOfClass(Frame.class, component);	
		}
		
		return ownerFrame;
	}

	/**
	 * @return the dialogWidth
	 */
	public int getDialogWidth() {
		return dialogWidth;
	}

	/**
	 * @return the dialogHeight
	 */
	public int getDialogHeight() {
		return dialogHeight;
	}
}
